package cn.xkai.exercise.b;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @description: 用栈实现队列
 * @author: kaixiang
 * @date: 2022-07-21
 **/
public class Solution83 {
    private Deque<Integer> stack1;
    private Deque<Integer> stack2;

    public Solution83() {
        stack1 = new ArrayDeque<>();
        stack2 = new ArrayDeque<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        fillStack();
        return stack2.pop();
    }

    public int peek() {
        fillStack();
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }

    private void fillStack() {
        if (stack2.isEmpty()) {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
    }
}
